/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.bath.domains.prey;

import java.util.Vector;
import uk.ac.bath.domains.misc.Limited2DSpace;
import uk.ac.bath.util.MyRandom;

/**
 *
 * @author pjl
 */
public class PreySpace extends Limited2DSpace {

//    protected Cell cellArray[][];
    Vector<Wall> walls = new Vector<Wall>();
    int popCount;
    MyPoint diff = new MyPoint();
    MyPoint home;

    public PreySpace(int sizeX, int sizeY,boolean withWalls) {
        super(sizeX, sizeY);
        home = new MyPoint(sizeX / 2, sizeY / 2);


        int x1 = sizeX / 3;
        int x2 = sizeX - x1;

        int y1 = sizeY / 3;
        int y2 = sizeY - y1;

       

        if (withWalls) {
            walls.add(new Wall(new MyPoint(x1,y1),  new MyPoint(x2,y1)));
            walls.add(new Wall(new MyPoint(x2, y1), new MyPoint(x2, y2)));
          //  walls.add(new Wall(new MyPoint(x2, y2), new MyPoint(x1, y2)));
          //  walls.add(new Wall(new MyPoint(x1, y2), new MyPoint(x1, y1)));
        }
    }

    @SuppressWarnings("static-access")
    void update(PreyViewable env) {

        // hunter gets to move first  . .

        HunterCell hunter = env.getHunter();
        PreyCell prey = env.getPrey();

        MyPoint proj = hunter.nextPointMayBe();

        if (contains(proj) && legalMove((MyPoint) hunter.getPoint(), proj)) {
            hunter.moveTo(proj);
        } else {
            hunter.collide();
        }


        if (!hunter.hasCaptured()) {
            MyPoint.diff((MyPoint) hunter.getPoint(), (MyPoint) prey.getPoint(), diff);

            if (MyPoint.abs(diff) < 3) {
                //  System.out.println(" Got you");
                //  hunter.fitness+=env.initFit.doubleValue();
                hunter.capturePrey();
                MyPoint p = randomFreePoint();
                prey.moveTo(p);
            }
        }

        if (hunter.hasCaptured()) {
            MyPoint.diff((MyPoint) hunter.getPoint(), home, diff);

            if (MyPoint.abs(diff) < 3) {
                //   System.out.println(" Got home ");
                hunter.fitness += HunterCell.initFitness.doubleValue();
                hunter.devourPrey();
            //          MyPoint p = randomFreePoint();
            //          prey.moveTo(p);
            }
        }

        proj = prey.nextPointMayBe();

        if (contains(proj) && legalMove((MyPoint) prey.getPoint(), proj)) {
            prey.moveTo(proj);
        } else {
            prey.collide();
        }


        if (legalMove((MyPoint) prey.getPoint(), (MyPoint) hunter.getPoint())) {
            hunter.setPreyScanner(prey.getMyPoint());
        } else {
            hunter.setPreyScanner(null);
        }

        if (legalMove((MyPoint) hunter.getPoint(), home)) {
            hunter.setHomeScanner(home);
        } else {
            hunter.setHomeScanner(null);
        }

    }

    MyPoint randomFreePoint() {
        float x;
        float y;
        //    do {
        x = (float) (MyRandom.nextDouble() * sizeX);
        y = (float) (MyRandom.nextDouble() * sizeY);

        //    } while (cellArray[x][y] != null);

        return new MyPoint(x, y);
    }

    private boolean legalMove(MyPoint point, MyPoint proj) {
        Line l = new Line(point, proj);
        for (Wall w : walls) {
            if (w.intersectsWith(l)) {
                return false;
            }
        }
        return true;
    }

//    public void remove(HunterCell hunter) {
//        cellArray[hunter.getPoint().getIX()][hunter.getPoint().getIY()] = hunter;
//    }
}
